//index.js
//获取应用实例
const app = getApp()
var util= require("../../utils/libs.js")
var api = require("../../utils/api.js")
var data = require("../../utils/date.js")
Page({
  data: {
    id: null,
    show:true,
    detail: {},
    isSave:true,
    star:0,
    article_content: "",
    uid:null,
    saveText:"收藏",
    shareI:false,
    shareImage:'',
    title:'',
    user:'',
    description:''
  },
  commit:function(event){
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../commit/commit?id='+id,
    })
  },
  handleClick(){
   let that = this
   that.setData({
     show:false
   })
  },
  thumb:function(){
    wx.navigateTo({
      url: '../groups/groups',
    })
  },
  showDetail(){
    let that = this
    util.request(api.topicArticleDetail,{
      id:that.data.id,
      uid:that.data.uid
    },"GET")
    .then(function (res) {
      if(res.error_code === 0){
        console.log(res)
        let arr = res.data.article
        arr.create_time = data.year(arr.create_time)
        that.setData({
          detail:arr,
          user:res.data.article.user,
          star:res.data.star,
          title:arr.title,
          description:arr.content.substring(0,30)
        })
      }
    })

  },
  tothink(){
    let that = this
    util.request(api.topicDoLike,{
    id:that.data.id,
    uid:that.data.uid
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  toSave(){
    let that = this
    if(that.data.isSave){
      util.request(api.topicdoStar,
        {id:that.data.id,
        uid:that.data.uid
      },"POST")
      .then(function (res) {
        if(res.error_code === 0){
            that.setData({
              isSave:false,
              saveText:"取消收藏"
            })
        }
      })
    }else{
      util.request(api.topiccancelStar,
        {id:that.data.id,
        uid:that.data.uid
      },"POST")
      .then(function (res) {
        if(res.error_code === 0){
            that.setData({
              isSave:true,
              saveText:"收藏"
            })
        }
      })
    }
  },
  share() {
    this.setData({
      shareI: true
    })
  },
  closeImg() {
    this.setData({
      shareI: false
    })
  },
  saveI(event) {
    var url = event.currentTarget.dataset.src
    console.log(url)
    wx.getSetting({
      success(res) {
        console.log(res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              wx.downloadFile({
                url: url,
                success(res) {
                  console.log(res)
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(res) {
                      console.log(res)
                      wx.showToast({
                        title: '保存成功',
                        icon: 'none',
                        duration: 2000
                      })
                      that.setData({
                        shareI: false
                      })
                    },
                    fail(res) {
                      console.log(res)
                    }
                  })
                }
              })
            }
          })
        } else {
          wx.downloadFile({
            url: url,
            success(res) {
              console.log(res)
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  console.log(res)
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    duration: 2000
                  })
                  that.setData({
                    shareI: false
                  })
                },
                fail(res) {
                  console.log(res)
                }
              })
            }
          })
        }
      },
      error(res) {
        console.log(res)
      }
    })
  },
  getShare() {
    var that = this
    wx.request({
      // 获取token
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential',
      data: {
        appid: 'wxb78b3211945cae67',
        secret: 'ad9e02f1ea741473fa5615787c0e9d7e'
      },
      success(res) {
        wx.request({
          // 调用接口C
          url: 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' + res.data.access_token,
          method: 'POST',
          data: {
            path: "pages/index/index",
            width: 430,
            auto_color: false,
            line_color: { r: 0, g: 0, b: 0 }
          },
          success(res) {
            let author = ''
            // res是二进制流，后台获取后，直接保存为图片，然后将图片返回给前台
            // 后台二进制怎么转图片？我也不会后台，学会了再贴代码
            if (that.data.user == null) {
              author = '未知'
            } else {
              author = that.data.user.nickname
            }
            util.request(api.upload, {
              type: 2,
              title: that.data.title,
              author: author,
              description: that.data.description
            }, 'POST').then(function (res) {
              that.setData({
                shareImage: res.data.file
              })
            })
          }
        })
      }
    })
  },
  onLoad(options){
    let that = this
    let uid = wx.getStorageSync('uid')
    that.setData({
      id: options.id,
      uid:uid 
    })
    that.showDetail()
    that.getShare()
  }
})
